package com.youlai.system.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.system.common.model.Option;
import com.youlai.system.common.result.PageResult;
import com.youlai.system.common.result.Result;
import com.youlai.system.model.form.GradeForm;
import com.youlai.system.model.query.GradePageQuery;
import com.youlai.system.model.vo.GradePageVO;
import com.youlai.system.plugin.dupsubmit.annotation.PreventDuplicateSubmit;
import com.youlai.system.service.SysGradeService;
import com.youlai.system.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 职级控制器
 */
@Tag(name = "20.职级接口")
@RestController
@RequestMapping("/api/v1/grades")
@RequiredArgsConstructor
public class SysGradeController {

    private final SysGradeService gradeService;

    private final SysUserService userService;

    @Operation(summary = "职级分页列表")
    @GetMapping("/page")
    public PageResult<GradePageVO> getGradePage(
            @ParameterObject GradePageQuery queryParams
    ) {
        IPage<GradePageVO> result = gradeService.getGradePage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增职级")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:grade:add')")
    @PreventDuplicateSubmit
    public Result saveGrade(
            @RequestBody @Valid GradeForm gradeForm
    ) {
        boolean result = gradeService.saveGrade(gradeForm);
        return Result.judge(result);
    }

    @Operation(summary = "职级表单数据")
    @GetMapping("/{gradeId}/form")
    public Result<GradeForm> getGradeForm(
            @Parameter(description = "职级ID") @PathVariable Long gradeId
    ) {
        GradeForm formData = gradeService.getGradeForm(gradeId);
        return Result.success(formData);
    }


    @Operation(summary = "修改职级")
    @PutMapping(value = "/{gradeId}")
    @PreAuthorize("@ss.hasPerm('sys:grade:edit')")
    public Result updateGrade(
            @Parameter(description = "职级ID") @PathVariable Long gradeId,
            @RequestBody @Validated GradeForm gradeForm) {
        boolean result = gradeService.updateGrade(gradeId, gradeForm);
        return Result.judge(result);
    }

    @Operation(summary = "删除职级")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:grade:delete')")
    public Result deleteGrades(
            @Parameter(description = "职级ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "职级删除数据为空");
        List<Long> idList = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        boolean gradeHasUsed = userService.gradeHasUsed(idList);
        Assert.isTrue(!gradeHasUsed, "职级使用中，不能被删除");
        boolean result = gradeService.deleteGrades(ids);
        return Result.judge(result);
    }

    @Operation(summary = "职级下拉列表")
    @GetMapping("/options")
    public Result<List<Option>> listGradeOptions() {
        List<Option> list = gradeService.listGradeOptions();
        return Result.success(list);
    }

}
